package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalCouponCategory;
import org.jeecg.modules.rental.entity.RentalCouponConfig;
import org.jeecg.modules.rental.service.IRentalCouponConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 卡券活动
 * @Author: jeecg-boot
 * @Date:   2024-08-19
 * @Version: V1.0
 */
@Api(tags="卡券活动")
@RestController
@RequestMapping("/rental/coupon-config")
@Slf4j
public class RentalCouponConfigController extends JeecgController<RentalCouponConfig, IRentalCouponConfigService> {
	@Autowired
	private IRentalCouponConfigService rentalCouponConfigService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rentalCouponConfig
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	//@AutoLog(value = "卡券活动-分页列表查询")
	@ApiOperation(value="卡券活动-分页列表查询", notes="卡券活动-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="rental/coupon/configlist")
	public Result<IPage<RentalCouponConfig>> queryPageList(RentalCouponConfig rentalCouponConfig, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
														   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
														   HttpServletRequest request) {
		QueryWrapper<RentalCouponConfig> queryWrapper = QueryGenerator.initQueryWrapper(rentalCouponConfig, request.getParameterMap());
		Page<RentalCouponConfig> page = new Page<RentalCouponConfig>(pageNo, pageSize);
		IPage<RentalCouponConfig> pageList = rentalCouponConfigService.selectJoinPage(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rentalCouponConfig
	 * @return
	 */
	@AutoLog(value = "卡券活动-添加")
	@ApiOperation(value="卡券活动-添加", notes="卡券活动-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RentalCouponConfig rentalCouponConfig) {
		rentalCouponConfigService.save(rentalCouponConfig);
		return Result.OK("common.add.success");
	}
	
	/**
	 *  编辑
	 *
	 * @param rentalCouponConfig
	 * @return
	 */
	@AutoLog(value = "卡券活动-编辑")
	@ApiOperation(value="卡券活动-编辑", notes="卡券活动-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RentalCouponConfig rentalCouponConfig) {
		rentalCouponConfigService.updateById(rentalCouponConfig);
		return Result.OK("common.edit.success");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "卡券活动-通过id删除")
	@ApiOperation(value="卡券活动-通过id删除", notes="卡券活动-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rentalCouponConfigService.removeById(id);
		return Result.OK("common.delete.success");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "卡券活动-批量删除")
	@ApiOperation(value="卡券活动-批量删除", notes="卡券活动-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rentalCouponConfigService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("common.delete.batch.success");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "卡券活动-通过id查询")
	@ApiOperation(value="卡券活动-通过id查询", notes="卡券活动-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RentalCouponConfig> queryById(@RequestParam(name="id",required=true) String id) {
		RentalCouponConfig rentalCouponConfig = rentalCouponConfigService.getById(id);
		if(rentalCouponConfig==null) {
			return Result.error("common.query.not.found",null);
		}
		return Result.OK(rentalCouponConfig);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rentalCouponConfig
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalCouponConfig rentalCouponConfig) {
        return super.exportXls(request, rentalCouponConfig, RentalCouponConfig.class, "卡券活动");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalCouponConfig.class);
    }


	 /**
	  * 小程序客户端使用
	  * @param rentalCouponConfig
	  * @return
	  */
	 @ApiOperation(value="卡券活动-列表查询", notes="卡券活动-列表查询")
	 @GetMapping(value = "/config-list")
	 public Result<List<RentalCouponConfig>> configList(RentalCouponConfig rentalCouponConfig) {
		 List<RentalCouponConfig> list = rentalCouponConfigService.configList(rentalCouponConfig);
		 return Result.OK(list);
	 }


}
